package com.shizhanzhe.zxbapp.my;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.TaskBean;
import com.shizhanzhe.zxbapp.bean.UserBean;
import com.shizhanzhe.zxbapp.url.Path;
import com.shizhanzhe.zxbapp.utils.OkHttpDownloadJsonUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TaskRecordActivity extends AppCompatActivity {

    @BindView(R.id.top_back)
    ImageButton topBack;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.srl)
    SmartRefreshLayout srl;
    private TaskRecordAdapter mAdapter;
    private int page = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_record);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .fitsSystemWindows(true)  //使用该属性,必须指定状态栏颜色
                .statusBarColor(R.color.white)
                .statusBarDarkFont(true, 0.2f)
                .init();
        topTitle.setText("任务记录");
        initData();
        setListener();
    }

    private void setListener() {
        topBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        srl.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                page++;
                taskList(UserBean.uid,page);
            }
        });
        srl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                page = 1;
                taskList(UserBean.uid,page);
            }
        });
    }
    private void initData(){
        srl.setRefreshHeader(new MaterialHeader(this));
        recycler.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new TaskRecordAdapter(this);
        recycler.setAdapter(mAdapter);
        taskList(UserBean.uid,page);
    }
    private void taskList(int uid, int cPage){
        OkHttpDownloadJsonUtil.downloadJson(TaskRecordActivity.this, Path.taskRecord + "?uid=" + uid + "&page=" + cPage, new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {

            @Override
            public void onsendJson(String json) {
                srl.finishRefresh();
                srl.finishLoadMore();
                if (!"1".equals(json) && !"2".equals(json)){
                    try {
                        Gson gson = new Gson();
                        TaskBean tiBean = gson.fromJson(json, TaskBean.class);
                        if (tiBean.getData().getData().size() > 0) {
                            mAdapter.updateData(page, tiBean.getData().getData());
                        } else {
                            if (page == 1) {
                                Toast.makeText(TaskRecordActivity.this, "暂无任务记录", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(TaskRecordActivity.this, "没有更多任务记录了", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }catch(Exception e){
                    }
                }else {
                    Toast.makeText(TaskRecordActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
